package com.jdew.thkj.activity;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jdew.thkj.R;
import com.jdew.thkj.manager.ActivityManagerUtils;
import com.jdew.thkj.manager.ZoomViewHolder;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EnlargePreviewActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.rl_dots)
    LinearLayout rlDots;
    @Bind(R.id.banner)
    MZBannerView banner;
    @Bind(R.id.back)
    ImageView back;
    private List<String> imageList;
    private List<ImageView> listshape = new ArrayList<>();
    private ImageView lastImage;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        changeStatusBarTextColor(true);
        setContentView(R.layout.activity_enlarge_preview);
        ButterKnife.bind(this);
        ActivityManagerUtils.pushActivity(this);
        imageList = getIntent().getStringArrayListExtra("imageList");
        position = getIntent().getIntExtra("position", 0);
        back.setOnClickListener(this);
        initshape();
    }

    //初始化小圆点的方法
    private void initshape() {
        banner.setPages(imageList, new MZHolderCreator<ZoomViewHolder>() {
            @Override
            public ZoomViewHolder createViewHolder() {
                return new ZoomViewHolder();
            }
        });
        banner.setIndicatorVisible(false);
        banner.getViewPager().setCurrentItem(position);
        banner.start();
        banner.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (lastImage != null) {
                    lastImage.setImageResource(R.drawable.gray_dot);
                }
                listshape.get(position).setImageResource(R.drawable.light_dot);
                lastImage = listshape.get(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        for (int i = 0; i < imageList.size(); i++) {
            ImageView imageView = new ImageView(this);
            if(imageList.size()==1){
                rlDots.setVisibility(View.GONE);
            }
            if (i == 0) {
                //如果当前是第一页,就设置选中的图片
                imageView.setImageResource(R.drawable.light_dot);
                lastImage = imageView;
            } else {
                imageView.setImageResource(R.drawable.gray_dot);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(9, 0, 9, 0);
            //添加到集合和布局里
            listshape.add(imageView);
            rlDots.addView(imageView, layoutParams);
            if (i == position) {
                listshape.get(i).setImageResource(R.drawable.light_dot);
                lastImage = listshape.get(i);
            } else {
                listshape.get(i).setImageResource(R.drawable.gray_dot);
            }
        }
    }


    @Override
    protected void onDestroy() {
        listshape.clear();
        if (rlDots != null) {
            rlDots.removeAllViews();
        }
        super.onDestroy();
        ButterKnife.unbind(this);
        ActivityManagerUtils.popActivity(this);
    }

    /**
     * 沉浸式状态栏
     */
    private void changeStatusBarTextColor(boolean isBlack) {
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = this.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.color_000000));
            if (isBlack) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            } else {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
        }
    }
}
